package com.tgram.android_mapapi.map;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.esri.android.map.Callout;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Point;
import com.tgram.android_mapapi.R;

/**
 * @className: MapManager
 * @description: Acrgis鍦板浘绠＄悊绫�1. 鏀寔鍙几缂╃殑閰嶇疆锛�2. 鏀寔鍦烘櫙锛屽崟鍦板浘鐨�
 * @author terry_zz
 * @date 2013-6-6 涓嬪崍2:45:53
 */
public class MapManager implements OnStatusChangedListener {

	private static final String TAG = MapManager.class.getSimpleName();

	private static final long serialVersionUID = 1L;

	private MapView mapView;
	// private Context mCtx;
	private GraphicsLayerBase mGraphicsLayer; // 鍩虹鍥惧舰鍖栧浘灞�
	// private GraphicsLayer mGraphicsLayerCorrect;//鐐逛綅绾犻敊鍥惧眰
	private Callout mCallout;
	private Map<String, GraphicsLayerBase> mLayers = new HashMap<String, GraphicsLayerBase>(); // 闆嗗悎鍥惧眰

	private int mLevel = 0;

	private OnMapStatusChanged mOnMapStatusChangedListener;
	private OnMapZoomChanged mOnMapZoomChangedListener;
	private Context context;

	public interface OnMapStatusChanged {
		public void onStatusChanged(Object source, STATUS status);

		public void onMapInitialized(MapView mapView);
	};

	public interface OnMapZoomChanged {
		public void onZoomChanged(int level);
	};

	public MapManager(Context context, MapView mapView) {
		this.mapView = mapView;
		this.context = context;
		// this.mCtx = context;
		mGraphicsLayer = new GraphicsLayerBase();
		// init callout
		mCallout = mapView.getCallout();
		mCallout.setStyle(R.xml.pop_mapitem);
		mCallout.setOffset(0, 15); // 榛樿鍊硷紝鍙互鏇存敼
	}

	@Override
	public void onStatusChanged(Object source, STATUS status) {
		if (mOnMapStatusChangedListener == null) {
			Log.e(TAG, "can't find map status listener:");
			return;
		} else {
			mOnMapStatusChangedListener.onStatusChanged(source, status);
			if (mapView == null) {
				// 鏈缃湴鍥捐浇浣�
			} else {
				if (source == mapView && status == STATUS.INITIALIZED) {
					Log.v(TAG, "map initialized!!!");
					mOnMapStatusChangedListener.onMapInitialized(mapView);
				} else if (status == STATUS.INITIALIZATION_FAILED) {
				}
			}
		}
	}

	/**
	 * 鍒濆鍖栧熀纭�浘灞�榛樿甯︽湁涓�釜鍥惧舰鍖栧浘灞�
	 * 
	 * @param layer
	 * @return
	 */
	public MapManager initBaseLayer(Layer layer) {
		mapView.removeAll();
		mapView.addLayer(layer);
		mapView.setMapBackground(0xe6e6e6, 0x00ffffff, 1.0f, 1.0f);
		mapView.addLayer(mGraphicsLayer);
		return this;
	}

	/**
	 * 娣诲姞鍥惧眰
	 * 
	 * @param key
	 * @param layer
	 */
	public void addLayer(String key, GraphicsLayerBase layer) {
		GraphicsLayerBase layerOld = mLayers.put(key, layer);
		if (layerOld == null) {
			mapView.addLayer(layer);
		}
	}

	/**
	 * 寮�Map,鐪熸鐨勫紑濮嬫槸map.unPause(),杩欓噷涓轰簡鎺у埗Map鍦ㄥ姞杞藉畬鎴愪互鍚庝娇鐢�
	 */
	public void startMap() {
		this.mapView.setOnStatusChangedListener(this);
	}

	/**
	 * 鍦板浘鍔犺浇浜嬩欢
	 * 
	 * @param onMapStatusChanged
	 */
	public void setOnMapStatusChangedListener(
			OnMapStatusChanged onMapStatusChanged) {
		this.mOnMapStatusChangedListener = onMapStatusChanged;
	}

	/**
	 * 鍦板浘缂╂斁浜嬩欢
	 * 
	 * @param onMapZoomChanged
	 */
	public void setOnMapZoomChangedListener(OnMapZoomChanged onMapZoomChanged) {
		this.mOnMapZoomChangedListener = onMapZoomChanged;
	}

	/**
	 * 璁剧疆鍦板浘缂╂斁绾у埆
	 * 
	 * @param level
	 */
	public void setZoomLevel(int level) {
		Point _Point = mapView.getCenter();
		if (_Point != null) {//
			setZoomLevel(level, _Point);
		} else {
			Log.e(TAG, "map not Initialized,map don't have center point!");
		}
	}

	/**
	 * 璁剧疆涓績鐐瑰苟涓斿埗瀹氱缉鏀剧骇鍒�
	 * 
	 * @param level
	 * @param centerPoint
	 */
	public void setZoomLevel(int level, Point centerPoint) {
		// TODO 涓栫邯瀹夎蒋闇�鍗曠嫭璁剧疆涓績鐐圭殑鎺ュ彛
		if (mLevel != level) {
			mLevel = level;
			mapView.zoomToResolution(centerPoint, MapUtil.getResolution(level));
			if (mOnMapZoomChangedListener != null) {
				mOnMapZoomChangedListener.onZoomChanged(level);
			}
		}
	}

	/**
	 * 鑾峰彇鍦板浘缂╂斁绾у埆
	 */
	public int getZoomLevel() {

		return mLevel;
	}

	public MapView getMapView() {
		return mapView;
	}

	/**
	 * 鑾峰彇MapView鐨勫浘灞傦紝榛樿涓哄睘鎬у浘灞�
	 * 
	 * @return
	 */
	public GraphicsLayerBase getLayer() {
		return mGraphicsLayer;
	}

	/**
	 * 杩斿洖鎸囧畾鐨勫浘灞�
	 * 
	 * @param key
	 *            鎸囧畾鍥惧眰鐨凨ey鍊�
	 * @return
	 */
	public GraphicsLayerBase getLayer(String key) {
		if (key == null) {
			return null;
		}
		GraphicsLayerBase layer = mLayers.get(key);
		return layer;
	}

	public Callout getCallout() {
		return mCallout;
	}

	/**
	 * 鍦板浘绾犻敊瀹屾垚
	 */
	public void pointPosCorrected() {
		getLayer(GraphicLayerCorrect.CORRECT_LAYER).removeAll();
		getLayer(GraphicLayerCorrect.CORRECT_LAYER).setVisible(false);
		getLayer().setVisible(true);
	}
}
